//
//  HomeController.m
//  LingQian
//
//  Created by xieyingze on 2018/1/21.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "HomeController.h"
#import "HomeEarningCell.h"
#import "HomeHeaderView.h"
#import "HomeButtonsCell.h"
#import "HomeInvestBindCell.h"
#import "HomeViewModel.h"

@interface HomeController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) HomeHeaderView *headerView;
@property (nonatomic, strong) HomeViewModel *viewModel;

@end

@implementation HomeController

#pragma mark - *********************生命周期*********************

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - *********************基础配置*********************

- (void)configuration {
    self.navigationController.navigationBar.hidden = YES;
    [self hideCustomNavView];
}

- (void)initSubViews {
    [self.view addSubview:self.tableView];
}

- (void)autoLayoutForSubViews {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(20, 0, 0, 0));
    }];
}

- (void)bindViewModel {
    @weakify(self);
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kNotifi_homeRefresh object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
    }];
    
    self.tableView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, kUI_HEIGHT);
    [UIView animateWithDuration:1 delay:0.25 usingSpringWithDamping:5 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.tableView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0);
    } completion:nil];
}

#pragma mark - *********************基础方法*********************


#pragma mark - *********************代理方法*********************

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

//返回Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HomeEarningCell *cell = [HomeEarningCell cellWithTableView:tableView indexPath:indexPath];
        cell.viewModel = self.viewModel;
        return cell;
    } else if (indexPath.section == 1) {
        HomeButtonsCell *cell = [HomeButtonsCell cellWithTableView:tableView indexPath:indexPath];
        cell.viewModel = self.viewModel;
        return cell;
    }
    HomeInvestBindCell *cell = [HomeInvestBindCell cellWithTableView:tableView indexPath:indexPath];
    cell.viewModel = self.viewModel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 1 ? 110: 320;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 0 ? 80 : 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    self.headerView.viewModel = self.viewModel;
    return self.headerView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.headerView.offset = scrollView.contentOffset.y;
}

#pragma mark - *********************响应事件*********************


#pragma mark - *********************懒加载*********************

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.backgroundColor = kCOLOR_tableView;
        _tableView.rowHeight = 320;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (HomeHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [HomeHeaderView new];
    }
    return _headerView;
}

- (HomeViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [HomeViewModel new];
    }
    return _viewModel;
}

@end
